<template>
  <div class="table-box">
    <div class="table-search">
      <el-form :inline="true" :model="form">
        <el-form-item label="卡BIN">
          <el-input
            v-model="form.card_bin"
            placeholder="卡BIN"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="产品代码">
          <el-input
            v-model="form.product_code"
            placeholder="产品代码"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="发卡渠道">
          <el-select
            clearable
            v-model="form.issuer_channel"
            placeholder="发卡渠道"
          >
            <el-option
              v-for="item in issueChannelData"
              :key="item.value"
              :label="item.name"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否可用">
                <el-select clearable v-model="form.is_active" placeholder="是否可用">
                    <el-option label="否" :value="0" />
                    <el-option label="是" :value="1" />
                </el-select>
            </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onQuery">查询</el-button>
          <el-button @click="onReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="card content-box">
      <div class="table">
        <el-table :data="tableData" border v-loading="loading">
          <el-table-column prop="id" label="id" align="center" />
          <el-table-column
            prop="issuer_channel"
            label="发卡渠道"
            align="center"
          />
          <el-table-column
            prop="product_code"
            label="产品Code"
            align="center"
          />
          <el-table-column prop="card_bin" label="卡BIN" align="center" />
          <el-table-column prop="card_share" label="卡种类" align="center" />
          <el-table-column prop="is_active" label="是否可用" align="center">
            <template #default="scope">
              <el-tag
                :type="scope.row.is_active == 1 ? 'success' : 'danger'"
                >{{ scope.row.is_active === 1 ? "是" : "否" }}</el-tag
              >
            </template>
          </el-table-column>
          <el-table-column prop="currency" label="币种" align="center" />
          <el-table-column prop="card_scene" label="适用场景" align="center" />
          <el-table-column
            prop="product_intro"
            label="产品介绍"
            align="center"
          />
          <el-table-column
            prop="card_network"
            label="发卡网络"
            align="center"
          />
          <el-table-column
            prop="issue_region_code"
            label="发卡地区"
            align="center"
          />
          <el-table-column prop="three_ds" label="3DS验证" align="center">
            <template #default="scope">
              <el-tag :type="scope.row.three_ds == 1 ? 'success' : 'danger'">{{
                scope.row.three_ds === 1 ? "支持" : "不支持"
              }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="open_recharge" label="开卡充值" align="center">
            <template #default="scope">
              <el-tag
                :type="scope.row.open_recharge == 1 ? 'success' : 'danger'"
                >{{ scope.row.open_recharge === 1 ? "支持" : "不支持" }}</el-tag
              >
            </template>
          </el-table-column>
          <el-table-column
            prop="allow_transfer_in"
            label="卡充值"
            align="center"
          >
            <template #default="scope">
              <el-tag
                :type="scope.row.allow_transfer_in == 1 ? 'success' : 'danger'"
                >{{
                  scope.row.allow_transfer_in === 1 ? "支持" : "不支持"
                }}</el-tag
              >
            </template>
          </el-table-column>
          <el-table-column
            prop="allow_transfer_out"
            label="卡提现"
            align="center"
          >
            <template #default="scope">
              <el-tag
                :type="scope.row.allow_transfer_out == 1 ? 'success' : 'danger'"
                >{{
                  scope.row.allow_transfer_out === 1 ? "支持" : "不支持"
                }}</el-tag
              >
            </template>
          </el-table-column>
          <el-table-column prop="allow_freeze" label="卡冻结" align="center">
            <template #default="scope">
              <el-tag
                :type="scope.row.allow_freeze == 1 ? 'success' : 'danger'"
                >{{ scope.row.allow_freeze === 1 ? "支持" : "不支持" }}</el-tag
              >
            </template>
          </el-table-column>
          <el-table-column prop="allow_unfreeze" label="卡解冻" align="center">
            <template #default="scope">
              <el-tag
                :type="scope.row.allow_unfreeze == 1 ? 'success' : 'danger'"
                >{{
                  scope.row.allow_unfreeze === 1 ? "支持" : "不支持"
                }}</el-tag
              >
            </template>
          </el-table-column>
          <el-table-column prop="allow_cancel" label="卡注销" align="center">
            <template #default="scope">
              <el-tag
                :type="scope.row.allow_cancel == 1 ? 'success' : 'danger'"
                >{{ scope.row.allow_cancel === 1 ? "支持" : "不支持" }}</el-tag
              >
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="120" fixed="right">
              <template #default="scope">
                  <el-button type="primary" plain @click="handleOperate(scope.$index, scope.row, 'edit')">编辑</el-button>
              </template>
          </el-table-column>
        </el-table>
        <div class="table-pagination">
          <el-pagination
            :current-page="pageable.pageNum"
            :page-size="pageable.pageSize"
            :page-sizes="[10, 25, 50, 100]"
            :background="true"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageable.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          ></el-pagination>
        </div>
      </div>
    </div>
    <DialogForm
      @cancel="cancel"
      @closeDialog="closeDialog"
      @submitForm="submit"
      :dialogFormVisible="dialogVisible"
      :dialogForm="dialogForm"
      :btnLoading="btnLoading"
    />
  </div>
</template>

<script setup>
import { ref, onMounted, reactive, computed } from "vue";
import { cardsProductApi, cardsProductUpdateApi } from "@/api/payServerse/cards";
import DialogForm from "./components/cardProd.vue";
import { GlobalStore } from "@/store";

const globalStore = GlobalStore();
const payMerchantList = computed(() => globalStore.payMerchantList);
const loading = ref(false);
const tableData = ref([]);
const dialogVisible = ref(false);
const btnLoading = ref(false);
const btnType = ref(null);
const formData = {
  card_bin: "",
  product_code: "",
  issuer_channel: "",
  is_active: ""
};
const form = reactive({ ...formData });
const pageable = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0,
});
const dialogFormData = {
  id: "",
  is_active: ""
};
const dialogForm = reactive({ ...dialogFormData });
const issueChannelData = ref([]);
// 参数
const param = () => {
  return {
    page: pageable.pageNum,
    pagesize: pageable.pageSize,
    ...form,
  };
};
// 获取表格数据
const getTbaleData = async () => {
  loading.value = true;

  try {
    const res = await cardsProductApi(param());
    loading.value = false;
    tableData.value = res.data.results;
    issueChannelData.value = res.data.extra.issuer_channel;
    pageable.total = res.data.count;
  } catch (error) {
    loading.value = false;
  }
};
// 查询
const onQuery = () => {
  console.log("onQuery");
  getTbaleData();
};
// 重置
const onReset = () => {
  Object.assign(form, formData);
  getTbaleData();
};
//关闭弹窗
const closeDialog = () => {
  dialogVisible.value = false;
  btnType.value = null;
  Object.assign(dialogForm, dialogFormData);
};

// 取消
const cancel = () => {
  dialogVisible.value = false;
  btnType.value = null;
  Object.assign(dialogForm, dialogFormData);
};
// 提交
const submit = async (param) => {
  delete param.src;
  try {
    btnLoading.value = true;
    if (btnType.value == "edit") {
      const res = await cardsProductUpdateApi(param);
      ElMessage({
        message: res.message,
        type: "success",
      });
    }

    dialogVisible.value = false;
    btnLoading.value = false;
    getTbaleData();
  } catch (error) {
    btnLoading.value = false;
    //    dialogVisible.value = false;
  }
};

const handleOperate = async (index, row, type) => {
  // 编辑
  if (type === "edit") {
    btnType.value = "edit";
    Object.assign(dialogForm, { ...row });
    dialogVisible.value = true;
  }
};

// 条数
const handleSizeChange = (number) => {
  pageable.pageSize = number;
  getTbaleData();
};
// 页码
const handleCurrentChange = (number) => {
  pageable.pageNum = number;
  getTbaleData();
};
onMounted(() => {
  getTbaleData();
});
</script>
